package com.whenjun.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.ifootbook.util.common.BackJson;
import com.ifootbook.util.common.CallResult;
import com.whenjun.common.LoggerParent;
import com.whenjun.dto.req.EventReq;
import com.whenjun.service.core.DoorService;
import com.whenjun.service.core.EventService;
import com.whenjun.service.impl.IscCameraServiceImpl;
import com.whenjun.service.impl.IscPersonServiceImpl;
import com.whenjun.task.TaskKq;
import com.whenjun.thread.AsyncServiceEvent;
import com.whenjun.util.ArtemisPostUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: whenjun
 * @Date: 2023/5/19 0019 14:49
 * @Description:
 */
@RequestMapping("/debug")
@RestController
@Api(tags = "调试")
public class DebuggingController extends LoggerParent {


    @Autowired
    private ArtemisPostUtil artemisPostUtil;


    @Autowired
    private EventService eventService;

    @Autowired
    private IscPersonServiceImpl iscPersonService;

    @Autowired
    private DoorService doorService;

    @Autowired
    private AsyncServiceEvent asyncServiceEvent;

    @Autowired
    private IscCameraServiceImpl iscCameraService;

    @Autowired
    private TaskKq taskKq;


    @PostMapping("eventSubscriptionByEventTypes")
    @ApiOperation("按事件类型订阅事件")
    public CallResult<?> eventSubscriptionByEventTypes(@RequestBody EventReq eventReq) {
        eventService.eventSubscriptionByEventTypes(eventReq);
        return BackJson.success();
    }

    @PostMapping("eventUnSubscriptionByEventTypes")
    @ApiOperation("按事件类型取消订阅事件")
    public CallResult<?> eventUnSubscriptionByEventTypes(@RequestBody EventReq eventReq) {
        eventService.eventUnSubscriptionByEventTypes(eventReq);
        return BackJson.success();
    }


    @PostMapping("eventSubscriptionView")
    @ApiOperation("查询订阅事件")
    public CallResult<JSONObject> eventSubscriptionView() {
        return BackJson.successObj(eventService.eventSubscriptionView());
    }

    @PostMapping("iscRequest")
    @ApiOperation("isc请求")
    public CallResult<JSONObject> iscRequest(HttpServletRequest request, @RequestBody JSONObject jsonObject, String path) {
        log.info("ISC查询——————{} ", jsonObject.toJSONString());
        JSONObject resp = (JSONObject) artemisPostUtil.callPostApi(path, jsonObject);
        return BackJson.successObj(resp);
    }

    @PostMapping("role")
    @ApiOperation("角色测试")
    @SaCheckPermission(value = "video", orRole = "admin")
    public CallResult<?> role(){
        return BackJson.success();
    }

    @PostMapping("syncPerson")
    @ApiOperation("同步isc人员")
    public CallResult<Integer> syncPerson(){
        return BackJson.success(iscPersonService.synPerson());
    }


    @PostMapping("syncDoor")
    @ApiOperation("同步isc门禁点")
    public CallResult<Integer> syncDoor(){
        return BackJson.success(doorService.syncDoor());
    }


    @PostMapping("syncCamera")
    @ApiOperation("同步isc监控点")
    public CallResult<Integer> syncCamera(){
        return BackJson.success(iscCameraService.syncCamera());
    }


    @PostMapping("asyncTest")
    @ApiOperation("asyncTest")
    public CallResult<?> asyncTest() throws InterruptedException {
        return BackJson.success();
    }

    @PostMapping("taskKq")
    @ApiOperation("taskKq")
    public CallResult<?> taskKq() {
        taskKq.kqDataCal();
        return BackJson.success();
    }
}
